<!DOCTYPE html>
<!--Copyright 2010 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.-->
<html>
<head>
    <script src="jsonrpc_server.js" type="text/javascript"></script>
    <script src="jsonrpc_client.js" type="text/javascript"></script>
    <script type="text/javascript">
    var receiverData;
    var receiverMeta;
    var receiverCB;
        
    function logResult(text) {
        resultList = document.getElementById("testResults");
        resultItem = document.createElement("li");
        resultItem.textContent = text;
        resultList.appendChild(resultItem);
    }
    
    function assert(expr, testName) {
        if (!expr) {
            logResult(testName + " failed.");
        } else {
            logResult(testName + " succeeded");
        }
    }
    
    function exceptionExpected(func, testName) {
        var exception = false;
        try {
            func();
        } catch(e) {
            exception = true;
        }
        if (!exception) {
            logResult(testName + " failed.");
            return false;
        } else {
            logResult(testName + " succeeded");
            return true;
        }
    }
    
    var called = false;
    var serverResult;
    function run() {
        var testserver = jsonrpc_server();
    
        var testobj = {count: 0}
        
        var subscription = [];
        testobj.add = function(meta, firstval, secondval) {
            if (typeof firstval == "undefined") {
                throw "why undefined";
            }
            meta.callback(firstval + secondval);
        };

        testobj.increment = function(meta) {
            this.count++;
            return this.count;
        };

        testobj.origin = function(meta) {
                serverResult = "example.com";
        };

        testobj.nometa = function(test) {
            return test;
        };
        
        testobj.subscribe = function(meta, number) {
          subscription[number] = meta.callback;
        };
        
        testobj.sendsub = function(meta, number, message) {
          if (typeof subscription[number] === "function") {
            subscription[number](message);
          }
        };

        testobj.registerAll = function() {
            testserver.register("add", this.add, this, true);
            testserver.register("increment", this.increment, this, true);
            testserver.register("origin", this.origin, this, true);
            testserver.register("nometa", this.nometa, this);
            testserver.register("subscribe", this.subscribe, this, true);
            testserver.register("sendsub", this.sendsub, this, true);
        };
        testobj.registerAll();
        
        var forceServerReceive;
        testserver.addMessageReceiver(function(messageCallback) {
            forceServerReceive = messageCallback;
        });
        
        
        var forceClientReceive;
        var testclient = jsonrpc_client(function(data) {
            forceServerReceive(data, {origin: "example.com"}, function(message) {
                forceClientReceive(message);
            });
        });
        
        testclient.addMessageReceiver(function(messageCallback) {
            forceClientReceive = messageCallback;
        });
        

        var defaultErrorCB = function() { logResult("errorcb unexpected"); };
        
        var resultA;
        testclient.call("add", [10, 5], function(result) { resultA = result; }, defaultErrorCB);
        assert(resultA == 15, "async test");
        
        var resultB;
        testclient.call("increment", [], function(result) { resultB = result; }, defaultErrorCB);
        var resultC;
        testclient.call("increment", [], function(result) { resultC = result; }, defaultErrorCB);
        assert(resultC - resultB == 1, "context/sync test");
        
        var resultD;
        testclient.call("nometa", ["red"], function(result) { resultD = result; }, defaultErrorCB);
        assert(resultD == "red", "nometa test");
        
        var resultE = false;
        testclient.call("origin", [], function(result) { resultE = true; }, defaultErrorCB);
        assert(!resultE && serverResult == "example.com", "no callback test");
        
        var resultF = false;
        var resultG;
        testclient.call("add", [], function(result) { resultF = true; }, function(result) { resultG = result; });
        assert(!resultF && resultG == "why undefined", "error callback test");
        
        
        receiver = getPostMessageReceiver("*");
        receiver(function(d, m, cb) { receiverData = d; receiverMeta = m; receiverCB = cb; });
        sender = getPostMessageSender(window, "*");
        sender("hello");
        setTimeout('assert(receiverData == "hello", "post message test");', 200);
        //setTimeout('receiverCB("blue")', 300);
        //setTimeout('assert(receiverData == "blue", "post message cb test");', 500);
        
        publicclient = testclient;
    }
    
    </script>
</head>
<body onload="run();">
<ul id="testResults">
</ul>
<div id="result"></div>
</body>
</html>